const Service = require('egg').Service;

class OrderService extends Service {

	async SMSToBeSent(para) {

		const sql = this.app.mysql.get('weshop');
		const result = await sql.query(
			'select ' + 
   			'o.product_code sale_code, ' + 
   			'o.code as order_code, ' +
   			//'nps.name as sale_name, ' + 
   			'o.title as sale_name, ' +
   			'o.attr_name, ' + 
   			//'pa.attr_name , ' +
   			'o.sms_template_id, ' +
   			'o.mobile, ' +
			'if(o.pay_time is null,DATE_FORMAT(o.create_time,"%Y-%m-%d %H:%i:%S"),DATE_FORMAT(o.pay_time,"%Y-%m-%d %H:%i:%S")) as smstimes, ' + 
			'o.num, ' +
			'nps.sms_diy, ' +
			'o.from_app_id, ' +
			'o.order_unique_code, ' +
			'o.order_unique_code_url, ' + 
			'o.company_code, ' +
			'np.out_sale_company ' + 
			'from ' +
			'`order` o ' +
			'inner JOIN new_product np  on o.product_code = np.code ' +
			'inner join new_product_share nps on np.sale_code = nps.code ' +
			//'LEFT JOIN product_attr as pa  on o.attr_id = pa.id ' + 
			'where ' +
			'o.sms_state = "1" ' +
			'and ' +
			'o.ticket_state= "2" '
		);

		//console.log(result);

		return result;
	}



	async updateSmsState (code, s, sfrom) {

		const ss = sfrom || '1';

		const sql = this.app.mysql.get('weshop');
		const result = await sql.query(
			'update ' +
			'`order` ' + 
			'set ' + 
			'sms_state = ? ' +
			'where ' + 
			'sms_state = ? ' +
			'and code = ? ' , [s, ss, code]);

		return result.affectedRows === 1;
	}


	async getToBeRefundList () {

		const sql = this.app.mysql.get('weshop');
		let result = {};

		try{
			result = await sql.query(
				'select ' +
				'ob.id, ' +
				'ob.order_code, ' +
				'o.pay_trade_no, ' +
				'o.pay_fee, ' +
				'ob.back_price ' +
				//'o.uid, ' +
				//'o.company_code, ' +
				//'o.title ' +
				'from ' +
				'new_order_back ob, ' +
				'`order` o ' +
				'where ' +
				'ob.order_code = o.code ' +
				'and ob.ticket_state = "1" ' +
				'and ob.back_state = "0" ' + 
				'and ob.back_state_sub = "0" ' 
			);
		}catch(err) {
			this.app.logger.info(err);
			
		}
		

		return result;
	}

  	
}

module.exports = OrderService;

